package com.dycx.message.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.message.dto.TemplateDTO;
import com.dycx.message.entity.TemplateDO;
import com.dycx.message.enums.MessageTypeEnum;
import com.dycx.message.vo.TemplateVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息模版(Template)实体转换类
 *
 * @author wuzongbin
 * @since 2023-11-13 11:04:57
 */


@Data
public class TemplateConverter implements Serializable {
    public static TemplateDO dtoToEntity(TemplateDTO dto) {
        if (dto == null) return null;
        TemplateDO entity = BeanUtil.toBean(dto, TemplateDO.class);
        return entity;
    }

    public static TemplateVO entityToVo(TemplateDO entity) {
        if (entity == null) return null;
        TemplateVO vo = BeanUtil.toBean(entity, TemplateVO.class);

        vo.setMessageTypeName(MessageTypeEnum.converter(vo.getMessageType()));

        return vo;
    }

    public static List<TemplateVO> entityToVoList(List<TemplateDO> entityList) {
        List<TemplateVO> voList = new ArrayList<>();
        for (TemplateDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<TemplateVO> entityToVoPage(Page<TemplateDO> page) {

        List<TemplateVO> newRecords = new ArrayList<>();

        List<TemplateDO> records = page.getRecords();
        for (TemplateDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<TemplateVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
